package business.sale.entities;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;

import sale.entities.ArticleBillvo;
import sale.entities.FacturaVO;
import business.client.entities.Client;


public class Factura {

	private int primaryKey;
	private Client client;
	private Date date;
	private ArrayList<ArticleBill> arts;
	private double total, totalConDesc; // con descuento
	private double descuento;
	private Sale venta;

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
		this.venta.setClient(client);
	}

	public Date getDate() {
		return date;		
	}
	public ArrayList<ArticleBill> getArts() {
		return arts;
	}

	public double getTotal() {
		return total;
	}

	public int getPrimaryKey() {
		return primaryKey;
	}	

	public double getTotalConDesc() {
		return totalConDesc;
	}

	public double getDescuento() {
		return descuento;
	}

	public void setDescuento(double descuento) {
		this.descuento = descuento;
		BigDecimal aux = new BigDecimal(this.total);
		aux = aux.multiply(new BigDecimal(descuento/100));
		this.totalConDesc = aux.doubleValue();
	}
	public Sale getVenta() {
		return venta;
	}
	public Factura(FacturaVO fact) {
		if (fact != null) {
			this.primaryKey = fact.getPrimaryKey();
			this.client = new Client(fact.getClient());
			this.date = fact.getDate();
			this.total = fact.getTotal();
			this.arts = new ArrayList<ArticleBill>();
			for (ArticleBillvo o : fact.getArts()) {
				arts.add(new ArticleBill(o));
			}
			this.descuento = fact.getDescuento();
			this.totalConDesc = fact.getTotaltotalConDesc();
			this.venta = new Sale(fact.getSale());
		}
	}
	
	

	public Factura(int primaryKey, Client client, Date date,
			ArrayList<ArticleBill> arts, double total, double totalConDesc,
			double descuento, Sale venta) {
		this.primaryKey = primaryKey;
		this.client = client;
		this.date = date;
		this.arts = arts;
		this.total = total;
		this.totalConDesc = totalConDesc;
		this.descuento = descuento;
		this.venta = venta;
		this.venta.setCloseDate(date);
	}

	public FacturaVO toVO() {
		ArrayList<ArticleBillvo> temp = new ArrayList<ArticleBillvo>();
		for (ArticleBill o : this.arts)
			temp.add(o.toVO());
		return new FacturaVO(this.primaryKey, this.client.toVO(), this.date,
				temp, this.total, this.totalConDesc, this.descuento);
	}

}
